/*
 * THE SOURCE CODE AND ITS RELATED DOCUMENTATION IS PROVIDED "AS IS". INFINEON
 * TECHNOLOGIES MAKES NO OTHER WARRANTY OF ANY KIND,WHETHER EXPRESS,IMPLIED OR,
 * STATUTORY AND DISCLAIMS ANY AND ALL IMPLIED WARRANTIES OF MERCHANTABILITY,
 * SATISFACTORY QUALITY, NON INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * THE SOURCE CODE AND DOCUMENTATION MAY INCLUDE ERRORS. INFINEON TECHNOLOGIES
 * RESERVES THE RIGHT TO INCORPORATE MODIFICATIONS TO THE SOURCE CODE IN LATER
 * REVISIONS OF IT, AND TO MAKE IMPROVEMENTS OR CHANGES IN THE DOCUMENTATION OR
 * THE PRODUCTS OR TECHNOLOGIES DESCRIBED THEREIN AT ANY TIME.
 *
 * INFINEON TECHNOLOGIES SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
 * CONSEQUENTIAL DAMAGE OR LIABILITY ARISING FROM YOUR USE OF THE SOURCE CODE OR
 * ANY DOCUMENTATION, INCLUDING BUT NOT LIMITED TO, LOST REVENUES, DATA OR
 * PROFITS, DAMAGES OF ANY SPECIAL, INCIDENTAL OR CONSEQUENTIAL NATURE, PUNITIVE
 * DAMAGES, LOSS OF PROPERTY OR LOSS OF PROFITS ARISING OUT OF OR IN CONNECTION
 * WITH THIS AGREEMENT, OR BEING UNUSABLE, EVEN IF ADVISED OF THE POSSIBILITY OR
 * PROBABILITY OF SUCH DAMAGES AND WHETHER A CLAIM FOR SUCH DAMAGE IS BASED UPON
 * WARRANTY, CONTRACT, TORT, NEGLIGENCE OR OTHERWISE.
 *
 * (C)Copyright INFINEON TECHNOLOGIES All rights reserved
 */

package com.infineon.esim.lpa.util.android;

import androidx.lifecycle.Observer;

import com.infineon.esim.lpa.ui.profileDetails.ProfileDetailsActivity;
import com.infineon.esim.util.Log;

public class EventObserver<T> implements Observer<OneTimeEvent<T>> {
    private static final String TAG = ProfileDetailsActivity.class.getName();

    private final EventHandler<T> eventHandler;

    public EventObserver(EventHandler<T> eventHandler) {
        this.eventHandler = eventHandler;
    }

    @Override
    public void onChanged(OneTimeEvent<T> oneTimeEvent) {
        Log.debug(TAG,"Received an one-time event: " + oneTimeEvent);
        if(oneTimeEvent.hasBeenHandled()) {
            Log.debug(TAG,"The one-time event has already been handled. Nothing to do.");
        } else {
            Log.debug(TAG,"Handling the one-time event.");
            eventHandler.handleEvent(oneTimeEvent.getContentIfNotHandled());
        }
    }

    public interface EventHandler<T> {
        void handleEvent(T t);
    }
}
